layui.use(['form', 'layer', 'laydate', 'element'], function() {
	var form = layui.form();
	var $ = layui.jquery;
	var laydate = layui.laydate;
	var element = layui.element();
	var form = layui.form();
	$.ajax({
		type:"GET",
		url:url +'/ic-iot/devices/'+deviceId+'/record',
		async:true,
		dataType : 'json',
		async:false,
		beforeSend: function(request) {
            request.setRequestHeader("accessToken", accessToken);
        },
		success : function(data) {
			if(data['code'] == 1){
				$("#deviceNo").html(data.result.deviceNo);
				$("#deviceModel").html(data.result.deviceModel);
				if(data.result.deviceType == 0){
					$("#deviceType").html('GPRS');
				}else if(data.result.deviceType == 1){
					$("#deviceType").html('WIFI');
				};
				if(data.result.offlineAlarm == 1){
					$("#onofflineAlarm").attr('checked','checked');
					form.render();
				}else{
					$("#offofflineAlarm").attr('checked','checked');
					form.render();
				};
				if(data.result.overrunAlarm == 1){
					$("#onoverrunAlarm").attr('checked','checked');
					form.render();
				}else{
					$("#offoverrunAlarm").attr('checked','checked');
					form.render();
				};
				if(data.result.brokenAlarm == 1){
					$("#onbrokenAlarm").attr('checked','checked');
					form.render();
				}else{
					$("#offbrokenAlarm").attr('checked','checked');
					form.render();
				};
				if(data.result.batteryAlarm == 1){
					$("#onbatteryAlarm").attr('checked','checked');
					form.render();
				}else{
					$("#offbatteryAlarm").attr('checked','checked');
					form.render();
				};
				
				$("#deviceVersion").html(data.result.deviceVersion);
				$("#createTime").html(
					replaceTime(data.result.createTime / 1000)
				);
				$("#deviceName").val(data.result.deviceName);
				$("#deviceAddress").val(data.result.deviceAddress);
				$("#monitorPosition").val(data.result.monitorPosition);
				$("#monitorPosition").val(data.result.monitorPosition);
				$("#deviceDesc").html(data.result.deviceDesc);
				$("#step").val(data.result.step);
				$("#alarmStep").val(data.result.alarmStep);
				$("#temMinCh1").val(data.result.temMinCh1);
				$("#temMaxCh1").val(data.result.temMaxCh1);
				$("#humMaxCh1").val(data.result.humMaxCh1);
				$("#humMinCh1").val(data.result.humMinCh1);
				$("#temAdjustCh1").val(data.result.temAdjustCh1);
				$("#temMinCh2").val(data.result.temMinCh2);
				$("#temMaxCh2").val(data.result.temMaxCh2);
				$("#humMaxCh2").val(data.result.humMaxCh2);
				$("#humMinCh2").val(data.result.humMinCh2);
				$("#temAdjustCh2").val(data.result.temAdjustCh2);
				$("#deviceId").val(data.result.deviceId);
				$("#sceneId").val(data.result.sceneId); 
				form.render();
			}else{
				ifajax(data);
			}
		}
	});
	$("#button-keep").click(function(){
		var data = formToJson($("#form-one"));
		$.ajax({
			type:"put",
			url: url +'/ic-iot/devices/'+deviceId,
			data:data,
			dataType : 'json',
			async:false,
			contentType: "application/json; charset=utf-8",
			beforeSend: function(request) {
	            request.setRequestHeader("accessToken", accessToken);
	        },
			success : function(data) {
				if(data['code'] == 1){
					layer.msg(data['description']);
					setTimeout(function(){
				    	window.parent.location.reload();
				    },2000);
				}else{
					ifajax(data);
				}
			}
		});
	});
	$("#button-two").click(function(){
		var data = formToJson($("#form-two"));
		var deviceId = $("#deviceId").val();
		$.ajax({
			type:"put",
			url: url +'/ic-iot/users/'+adminId+'/devices/'+deviceId+'/setting',
			data:data,
			dataType : 'json',
			async:false,
			contentType: "application/json; charset=utf-8",
			beforeSend: function(request) {
	            request.setRequestHeader("accessToken", accessToken);
	        },
			success : function(data) {
				layer.msg(data['description']);
				if(data['code'] == 1){
					
					setTimeout(function(){
				    	window.parent.location.reload();
				    },2000);
					$('#deviceTable').dataTable().fnClearTable();
                	$('#deviceTable').dataTable().fnAddData(data.result);
				}else{
					ifajax(data);
				}
			}
		});
	});
	
	 /*添加地图*/
	   $('#map').on('click',  function() {
	    var username = '选择地址';
	    var href = 'map.html';
	    var index = layer.open({
	    	type:2,
	    	title:username,
	    	fixed:false,
	    	maxmin:false,
	    	area:['500px','400px'],
	    	content:href,
	    });
	    layer.full(index);
	  });

});
